package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dto.AllProductCateRequest;
import com.cskaoyan.shopping.dto.AllProductCateResponse;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IProductCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author CR
 * @since 2022/06/18 14:35
 */
@Service
public class IProductCateServiceImpl implements IProductCateService {
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ProductCateConverter cateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        AllProductCateResponse cateResponse = new AllProductCateResponse();
        try {
            request.requestCheck();
            Example example = new Example(ItemCat.class);
            example.setOrderByClause("sort_order ASC");
            // example.setOrderByClause(request.getSort());
            List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
            List<ProductCateDto> productCateDtos = cateConverter.items2Dto(itemCats);

            cateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            cateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            cateResponse.setProductCateDtoList(productCateDtos);

        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(cateResponse,e);
        }
        return cateResponse;
    }
}
